using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using QFramework;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class ChouseStart : MonoBehaviour
{
    public List<ToggleGroup> toggleGroups; // 引用多个 ToggleGroup
    // 定义枚举表示选项
    

    public async void StartButtonClicked()
    {
        ToastManager.Instance.ShowToast("创建对局中...");
        // 保存设置
        PlayerPrefs.SetString(GameMode.TiNumGroup, toggleGroups[0].GetFirstActiveToggle().GetComponentInChildren<Text>().text);
        PlayerPrefs.SetString(GameMode.AnsTimeGroup, toggleGroups[1].GetFirstActiveToggle().GetComponentInChildren<Text>().text);
        
        string mode = PlayerPrefs.GetString(GameMode.Mode);
        bool createSuccess = false;
        
        if (mode == GameMode.Single)
        {
            createSuccess = await CreateGameAsync(2); // 个人玩法
        }
        else if (mode == GameMode.Tuandui)
        {
            createSuccess = await CreateGameAsync(1); // 团队玩法
        }

        if(createSuccess)
        {
            ToastManager.Instance.ShowToast("创建对局成功");
            SceneManager.LoadScene(mode == GameMode.Single ? "singleMode" : "tuanduiMode");
        }
        else
        {
            Debug.LogError("创建对局失败，请重试");
            ToastManager.Instance.ShowToast("创建对局失败，请重试");
        }
    }

    // 修改方法返回bool表示是否成功
    public async Task<bool> CreateGameAsync(int GameType)
    {
        try
        {
            var Body = new
            {
                settings = new
                {
                    question_nums = PlayerPrefs.GetInt(GameMode.TiNumGroup),
                    answer_time = PlayerPrefs.GetInt(GameMode.AnsTimeGroup),
                },
                room_id = GlobalData.RoomId,
                type = GameType,
            };
            
            var postResponse = await HttpClientService.Instance
                .Post("/round/create")
                .SetBody(Body)
                .SendAsync();
            
            if (!postResponse.IsSuccess)
            {
                Debug.LogError($"Error: {postResponse.ErrorMessage}");
                return false;
            }

            RoundCreateResponse responseData = JsonUtility.FromJson<RoundCreateResponse>(postResponse.GetJson());
            Debug.Log($"整体状态码: {responseData.status}");
            Debug.Log($"对局id: {responseData.data.id}");
            GlobalData.SetRoundId(responseData.data.id.ToString());
            Debug.Log($"对接类型: {responseData.data.type}");
            Debug.Log($"房间id: {responseData.data.room_id}");
            Debug.Log($"内部状态: {responseData.data.status}");
            if(responseData.status == 401){
                StringEventSystem.Global.Send(EventID.ReLogin);
            }
            return responseData.status == 0; 
        }
        catch(Exception ex)
        {
            Debug.LogError($"CreateGameAsync异常: {ex.Message}");
            return false;
        }
    }

}
[System.Serializable]
public class RoundCreateResponse
{
    public int status;
    public ResponseData data;
}

[System.Serializable]
public class ResponseData
{
    public int id;
    public string type;
    public string room_id;
    public int status;
}
